/*
 * Filename:    HcpmsStorageServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-12-29
 */
package com.paic.mhis.hcpms.common.storage.biz.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.storage.biz.service.StorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDataDTO;
import com.paic.mhis.hcpms.common.storage.util.StorageContentUtil;

@Service("hcpmsStorageService")
public class HcpmsStorageServiceImpl implements HcpmsStorageService {
	@Autowired
	private StorageService storageService;

	@Override
	public void saveStorageData(HcpmsStorageDTO vo, boolean isMain) {
		HcpmsStorageDataDTO dto = setQueryStorageDateDto(vo);
		HcpmsStorageDataDTO resultDto = storageService.getStorageData(dto);
		if (resultDto != null) {
			resultDto.setUpdatedBy(vo.getUserId());
		} else {
			resultDto = dto;
			resultDto.setUpdatedBy(vo.getUserId());
			resultDto.setCreatedBy(vo.getUserId());
			resultDto.setIsValid("0");
			if (isMain) {
				resultDto.setIsMainFlag("Y");
			} else {
				resultDto.setIsMainFlag("N");
			}
		}
		if (vo.getDataMap() != null) {
			createStorageData(resultDto, vo.getDataMap());
		}
		if (vo.getUpdateCode() != null && !"".equals(vo.getUpdateCode())) {
			resultDto.setUpdateCode(vo.getUpdateCode());
		}
		if (vo.getDeleteCode() != null && !"".equals(vo.getDeleteCode())) {
			resultDto.setDeleteCode(vo.getDeleteCode());
		}
		// resultDto.setDataBlob(JSONObject.toJSONBytes(vo.getDataMap()));
		// if (!"".equals(vo.getParentId()) && vo.getParentId() != null) {
		// resultDto.setParentId(vo.getParentId());
		// }
		if (!"".equals(resultDto.getStorageId()) && resultDto.getStorageId() != null) {
			storageService.updateStorageDataDto(resultDto);
		} else {
			storageService.insertStorageDataDto(resultDto);
		}
	}

	public void createStorageData(HcpmsStorageDataDTO dto, Object obj) {
		// if (obj instanceof JSONObject || obj instanceof JSONArray) {
		// dto.setDataBlob(JSONObject.toJSONBytes(obj));
		// dto.setObjectType(StorageContentUtil.OBJECT_TYPE_JSON);
		// } else {
		// byte[] bytes=JSONObject.toJSONBytes(obj);
		// bytes.
		dto.setDataBlob(JSONObject.toJSONBytes(obj));
		dto.setObjectType(StorageContentUtil.OBJECT_TYPE_JSON);
		// }

	}

	public HcpmsStorageDataDTO setQueryStorageDateDto(HcpmsStorageDTO vo) {
		HcpmsStorageDataDTO dto = new HcpmsStorageDataDTO();
		if (!"".equals(vo.getBusinessId()) && vo.getBusinessId() != null) {
			dto.setBusinessId(vo.getBusinessId());
		}
		if (!"".equals(vo.getBusinessType()) && vo.getBusinessType() != null) {
			dto.setBusinessType(vo.getBusinessType());
		}
		if (!"".equals(vo.getModuleType()) && vo.getModuleType() != null) {
			dto.setModuleType(vo.getModuleType());
		}
		if (!"".equals(vo.getChildModuleType()) && vo.getChildModuleType() != null) {
			dto.setChildModuleType(vo.getChildModuleType());
		}
		if (!"".equals(vo.getParentId()) && vo.getParentId() != null) {
			dto.setParentId(vo.getParentId());
		}
		if (!"".equals(vo.getUserId()) && vo.getUserId() != null) {
			dto.setBelongUser(vo.getUserId());
		}
		if (!"".equals(vo.getAttribute1()) && vo.getAttribute1() != null) {
			dto.setAttribute1(vo.getAttribute1());
		}
		if (!"".equals(vo.getAttribute2()) && vo.getAttribute2() != null) {
			dto.setAttribute2(vo.getAttribute2());
		}
		if (!"".equals(vo.getAttribute3()) && vo.getAttribute3() != null) {
			dto.setAttribute3(vo.getAttribute3());
		}
		if (!"".equals(vo.getAttribute4()) && vo.getAttribute4() != null) {
			dto.setAttribute4(vo.getAttribute4());
		}
		if (!"".equals(vo.getAttribute5()) && vo.getAttribute5() != null) {
			dto.setAttribute5(vo.getAttribute5());
		}
		if (!"".equals(vo.getParentId()) && vo.getParentId() != null) {
			dto.setParentId(vo.getParentId());
		}
		return dto;
	}

	@Override
	public Object getStorageDataMap(HcpmsStorageDTO vo) {
		HcpmsStorageDataDTO dto = setQueryStorageDateDto(vo);
		HcpmsStorageDataDTO resultDto = storageService.getStorageData(dto);
		if (resultDto != null) {
			return JSONObject.parse(resultDto.getDataBlob());
		}
		return null;
	}

	@Override
	public void deleteStorageData(HcpmsStorageDTO vo) {
		HcpmsStorageDataDTO dto = setQueryStorageDateDto(vo);
		storageService.deleteStorageDate(dto);
	}

	@Override
	public Object getStorageData(HcpmsStorageDTO vo, Object bean) {
		HcpmsStorageDataDTO dto = setQueryStorageDateDto(vo);
		HcpmsStorageDataDTO resultDto = storageService.getStorageData(dto);
		if (resultDto != null && resultDto.getDataBlob() != null) {
			Map<String, Object> map = (Map<String, Object>) JSONObject.parse(resultDto.getDataBlob());
			Iterator<String> set = map.keySet().iterator();
			while (set.hasNext()) {
				String key = set.next();
				Object objValue = map.get(key);
				if (PropertyUtils.isReadable(bean, key)) {
					if (objValue != null) {
						try {
							PropertyUtils.setProperty(bean, key, objValue);
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						} catch (NoSuchMethodException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return bean;
	}

	@Override
	public HcpmsStorageDTO getStorageData(HcpmsStorageDTO vo) {
		HcpmsStorageDataDTO dto = setQueryStorageDateDto(vo);
		HcpmsStorageDataDTO resultDto = storageService.getStorageData(dto);
		if (resultDto != null) {
			getToStorageDto(resultDto, vo);
			return vo;
		}
		return null;

	}

	@Override
	public List<HcpmsStorageDTO> getStorageDataList(HcpmsStorageDTO vo) {
		List<HcpmsStorageDTO> list = new ArrayList<HcpmsStorageDTO>();
		HcpmsStorageDataDTO dto = setQueryStorageDateDto(vo);
		List<HcpmsStorageDataDTO> resultList = storageService.getStorageDataList(dto);
		if (resultList != null && resultList.size() > 0) {
			for (HcpmsStorageDataDTO result : resultList) {
				HcpmsStorageDTO newDto = new HcpmsStorageDTO();
				getToStorageDto(result, newDto);
				list.add(newDto);

			}
		}
		return list;
	}

	public void getToStorageDto(HcpmsStorageDataDTO vo, HcpmsStorageDTO dto) {
		if (vo.getDataBlob() != null) {
			dto.setDataMap(JSONObject.parse(vo.getDataBlob()));
		}
		if (vo.getUpdateCode() != null && !"".equals(vo.getUpdateCode())) {
			dto.setUpdateCode(vo.getUpdateCode());
		}
		if (vo.getDeleteCode() != null && !"".equals(vo.getDeleteCode())) {
			dto.setDeleteCode(vo.getDeleteCode());
		}
		if (!"".equals(vo.getBusinessId()) && vo.getBusinessId() != null) {
			dto.setBusinessId(vo.getBusinessId());
		}
		if (!"".equals(vo.getBusinessType()) && vo.getBusinessType() != null) {
			dto.setBusinessType(vo.getBusinessType());
		}
		if (!"".equals(vo.getModuleType()) && vo.getModuleType() != null) {
			dto.setModuleType(vo.getModuleType());
		}
		if (!"".equals(vo.getChildModuleType()) && vo.getChildModuleType() != null) {
			dto.setChildModuleType(vo.getChildModuleType());
		}
		if (!"".equals(vo.getParentId()) && vo.getParentId() != null) {
			dto.setParentId(vo.getParentId());
		}
		if (!"".equals(vo.getBelongUser()) && vo.getBelongUser() != null) {
			dto.setUserId(vo.getBelongUser());
		}
		if (!"".equals(vo.getAttribute1()) && vo.getAttribute1() != null) {
			dto.setAttribute1(vo.getAttribute1());
		}
		if (!"".equals(vo.getAttribute2()) && vo.getAttribute2() != null) {
			dto.setAttribute2(vo.getAttribute2());
		}
		if (!"".equals(vo.getAttribute3()) && vo.getAttribute3() != null) {
			dto.setAttribute3(vo.getAttribute3());
		}
		if (!"".equals(vo.getAttribute4()) && vo.getAttribute4() != null) {
			dto.setAttribute4(vo.getAttribute4());
		}
		if (!"".equals(vo.getAttribute5()) && vo.getAttribute5() != null) {
			dto.setAttribute5(vo.getAttribute5());
		}
		if (!"".equals(vo.getParentId()) && vo.getParentId() != null) {
			dto.setParentId(vo.getParentId());
		}
	}

	@Override
	public Map<String, Object> queryStorageData(JSONObject json, String userId, String type) {
		HcpmsStorageDTO vo = JSONObject.toJavaObject(json, HcpmsStorageDTO.class);
		vo.setUserId(userId);
		Map<String, Object> map = new HashMap<String, Object>();
		if (StorageContentUtil.EDIT_RETURN_DATA.equals(type)) {
			map.put("result", getStorageDataMap(vo));
		} else if (StorageContentUtil.EDIT_RETURN_LIST.equals(type)) {
			map.put("result", getStorageDataList(vo));
		} else if (StorageContentUtil.EDIT_RETURN_VO.equals(type)) {
			map.put("result", getStorageData(vo));
		}
		map.put("massage", StorageContentUtil.QUERY_RETURN_TRUE);
		map.put("state", StorageContentUtil.QUERY_STATE_TRUE);
		return map;
	}

	@Override
	public Map<String, Object> saveJsonStorageData(JSONObject json, String userId, String type) {
		HcpmsStorageDTO vo = JSONObject.toJavaObject(json, HcpmsStorageDTO.class);
		vo.setUserId(userId);
		Map<String, Object> map = new HashMap<String, Object>();
		if (StorageContentUtil.EDIT_RETURN_VO.equals(type)) {
			saveStorageData(vo, false);
		}
		map.put("massage", StorageContentUtil.SAVE_RETURN_TRUE);
		map.put("state", StorageContentUtil.QUERY_STATE_TRUE);
		return map;
	}
}
